package cn.zn.modules.appoin.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.zn.framework.common.exception.RRException;
import cn.zn.framework.common.utils.Query;
import cn.zn.modules.appoin.dao.AtRuleMapper;
import cn.zn.modules.appoin.entity.AppoinTimeRuleEntity;
import cn.zn.modules.appoin.entity.AtRule;
import cn.zn.modules.appoin.entity.vo.AppoinWorkUserVO;
import cn.zn.modules.appoin.entity.vo.AtRuleVO;
import cn.zn.modules.appoin.service.AppoinTimeRuleService;
import cn.zn.modules.appoin.service.AtRuleService;
import cn.zn.modules.zzsys.inter.TeacherInter;
import cn.zn.modules.zzsys.inter.vo.TeacherInfoVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AtRuleServiceImpl extends ServiceImpl<AtRuleMapper, AtRule> implements AtRuleService {

    @Autowired
    AppoinTimeRuleService appoinTimeRuleService;
    @Autowired
    TeacherInter teacherInter;

    @Override
    public List<AppoinWorkUserVO> workerList(Map<String, Object> params) {
        // 当前总规则
        AppoinTimeRuleEntity appoinTimeRuleEntity = appoinTimeRuleService.queryUse();
        if (appoinTimeRuleEntity == null) {
            throw new RRException("未开启默认规则");
        }
        IPage<Object> page = new Query<>(params).getPage();
        // 查询某角色的用户
        List<TeacherInfoVO> teacherInfoVOS = teacherInter.queryByRole(appoinTimeRuleEntity.getWorkerRole(), null);
        Map<String, AppoinWorkUserVO> userInfoVOMap = teacherInfoVOS.stream().collect(Collectors.toMap(TeacherInfoVO::getWorkNo, o -> {
            AppoinWorkUserVO appoinWorkUserVO = new AppoinWorkUserVO();
            BeanUtils.copyProperties(o, appoinWorkUserVO);
            appoinWorkUserVO.setUserCode(o.getWorkNo());
            return appoinWorkUserVO;
        }, (o1, o2) -> o1, LinkedHashMap::new));
        if (userInfoVOMap.size() > 0) {
            // 查询用户的工作时间设置
            Map<String, AtRule> atRuleMap = super.list(new QueryWrapper<AtRule>().in("USER_CODE_", userInfoVOMap.keySet())).stream().collect(Collectors.toMap(AtRule::getUserCode, o -> o, (o1, o2) -> o1));
            atRuleMap.forEach((k, v) -> {
                userInfoVOMap.get(k).setRuleId(v.getId());
                userInfoVOMap.get(k).setInterval(v.getInterval());
                userInfoVOMap.get(k).setIsAuto(v.getIsAuto());
            });
        }
        return CollUtil.newArrayList(userInfoVOMap.values());
    }

    @Override
    public AtRule queryByUserCode(String userCode) {
        AtRule atRule = super.getOne(new QueryWrapper<AtRule>().eq("USER_CODE_", userCode));
        if (atRule == null) {
            atRule = AtRule.builder()
                    .interval(15)
                    .isAuto(false)
                    .userCode(userCode)
                    .build();
            super.save(atRule);
        }
        return atRule;
    }

    @Override
    public AtRuleVO queryRuleByUserCode(String userCode) {
        AtRule atRule = super.getOne(new QueryWrapper<AtRule>().eq("USER_CODE_", userCode));
        if (atRule == null) {
            atRule = AtRule.builder()
                    .interval(15)
                    .isAuto(false)
                    .userCode(userCode)
                    .build();
            super.save(atRule);
        }
        AtRuleVO vo = new AtRuleVO();
        BeanUtils.copyProperties(atRule, vo);
        // 查询人员姓名
        TeacherInfoVO teacherInfoVO = teacherInter.findByWorkNo(vo.getUserCode());
        vo.setRealName(teacherInfoVO.getName());
        return vo;
    }
}
